import express from "express";

import { User } from "../../model/init";
import { asyncHandler } from "../middleware/asyncHandler";
import md5 from "md5";
import { publishJwt } from "../../utils/jwt";
import {getErr} from "../middleware/getSendResult"
import { CODE } from "@/utils/const";
const router = express.Router();

router.post(
    "/login",
    asyncHandler(async (req, res) => {
        console.log("到这里了");
        let result = await User.findOne({
            where: {
                username: req.body.username,
                password: md5(req.body.password),
            },
            attributes: ["phone", "id", "username"],
        });
        if (result) {
            let { phone, id, username } = result?.dataValues;
            // res.cookie(tokenKey, )
            publishJwt(res, {
                userId: id,
                phone,
                username,
            });
            return result?.dataValues;
        }else{
            throw new Error( "用户名或者密码错误" )
        }

        
    })
);




export default router;
